/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package alcatelparser.modules;

import alcatelparser.tags.ITagParser;
import alcatelparser.errors.*;
import alcatelparser.tags.RecordEndTag;
import java.util.LinkedList;

/**
 *
 * @author geniuz
 */
public class TagFactory {
        public TagFactory()
    {
        _tagNumber = 0;
        _tOrder = new TagOrder();
        _startParseTagIndex = 0;
    }

    public final void setStartParseTagIndex(int i)
    {
        _startParseTagIndex = i;
    }

    public final ITagParser getTag(int i)
    {
        if(i == 0 && _tagNumber == 9)
        {}//throw new BodyNotPresentedException();
        String s = formatTag(i);
        for(LinkedList linkedlist = _tOrder.getList(); _tagNumber < linkedlist.size();)
        {
            ITagParser itagparser = (ITagParser)linkedlist.get(_tagNumber);
            _tagNumber++;
            if(s.equals(itagparser.getParseIdentityTag()))
                if(linkedlist.indexOf(itagparser) < _startParseTagIndex)
                {
                    //throw new ParseFailedException(I.I(7480));
                } else
                {
                    _lastParsedTag = itagparser;
                    return itagparser;
                }
            if(isRequiredTagNotExists(itagparser))
            {}//throw new RequiredTagNotPresentException((new StringBuilder()).append(I.I(7515)).append(itagparser.getClass().getSimpleName()).append(I.I(7531)).append(BuilderFactory.getBuilder().getRecordWroteAmount() + 1).append(I.I(467)).toString());
        }

        return null;
    }

    private boolean isRequiredTagNotExists(ITagParser itagparser)
    {
        if(_tOrder.getTagIndex(itagparser) < _startParseTagIndex)
            return false;
        if(_lastParsedTag != null)
        {
            if(_lastParsedTag.isChoiceTag() && itagparser.isChoiceTag())
                return false;
            if(itagparser.isChoiceTag())
                return false;
            if(!itagparser.isChoiceTag() && _lastParsedTag.isChoiceTag())
                return true;
        }
        return itagparser.isTagRequired();
    }

    private String formatTag(int i)
    {
        String s = Integer.toHexString(i).toUpperCase();
        if(s.length() == 1)
            return (new StringBuilder()).append(I.I(548)).append(s).toString();
        else
            return s;
    }

    public final boolean isRecordFinished(int ai[], int i)
    {
        if(_tagNumber < 39)
            return false;
        if(_tagNumber >= _tOrder.getList().size())
            return true;
        if(i >= ai.length)
            return true;
        ITagParser itagparser = _tOrder.getTag(_tagNumber);
        int j = AlgoUtil.getTag(ai, i);
        if(_tOrder.getTag(_tagNumber) instanceof RecordEndTag)
        {
            String s = Integer.toHexString(j);
            if(!((RecordEndTag)itagparser).getParseIdentityTag().equalsIgnoreCase(s))
                return true;
        }
        return !isNextTagPresented(j);
    }

    private boolean isNextTagPresented(int i)
    {
        int j = _tagNumber;
        String s = formatTag(i);
        while(j < _tOrder.getList().size()) 
        {
            ITagParser itagparser = _tOrder.getTag(j);
            j++;
            if(s.equals(itagparser.getParseIdentityTag()))
                return true;
        }
        return false;
    }

    public final void releaseResources()
    {
        _tagNumber = 0;
        _startParseTagIndex = TEXT_FILE_RECORD_PARSE_INDEX;
    }

    public final void setTagCounter(int i)
    {
        _tagNumber = i;
    }

    public final int getNextTag(ITagParser itagparser)
    {
        return _tOrder.getNextTag(itagparser);
    }

    public final boolean isHeaderIsParsing()
    {
        return _startParseTagIndex != TEXT_FILE_RECORD_PARSE_INDEX;
    }

    public static int TEXT_FILE_RECORD_PARSE_INDEX = 9;
    private int _tagNumber;
    private TagOrder _tOrder;
    private ITagParser _lastParsedTag;
    private int _startParseTagIndex;
    //private static final Logger LOG = Logger.getLogger(com/alcatel/algo/tag/TagFactory);
}
